package com.yearner.reviewplan.common.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yearner.reviewplan.common.enums.RedisEnum;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class RedisUtil {
    private final JedisUtil jedisUtil;
    private final Gson gson;
    private Gson parseGson;
    private static RedisUtil sRedisUtil = null;
    public RedisUtil(JedisUtil jedisUtil, Gson gson) {
        this.jedisUtil = jedisUtil;
        this.gson = gson;
        this.parseGson = new GsonBuilder().serializeNulls().create();
        sRedisUtil = this;
    }

    public static RedisUtil getsRedisUtil() {
        return sRedisUtil;
    }

    public boolean pushNormal(String key, String value) {
        jedisUtil.set(key, value);
        return true;
    }
    public void pushNormalExpire(String key, String value) {
        pushNormalExpire(key, value);
    }
    public void pushNormalExpire(String key, String value, int expireTime) {
        expireTime = (expireTime == -1) ? RedisEnum.NORMAL_EXPIRE : expireTime;
        jedisUtil.set(key, value);
        expire(key, expireTime);
    }
    public String getNormal(String key) {
        return jedisUtil.get(key);
    }

    public <T> T getParseObject(String key, Class<T> tClass) {
        String result = jedisUtil.get(key);
        return (result == null) ? null : parseGson.fromJson(result, tClass);
    }

    //这里只负责redis相关操作，添加、删除、设置过期时间

    public void pushMap(String mapKey, String key, Object value) {
        jedisUtil.hset(mapKey, key, parseGson.toJson(value));
    }
    public void pushMap(String mapKey, String key, String value) {
        jedisUtil.hset(mapKey, key, value);
    }

    public void pushMapExpire(String mapKey, String key, Object value) {
        pushMapExpire(mapKey, key, value, -1);
    }

    public void pushMapExpire(String mapKey, String key, Object value, int expireTime) {
        jedisUtil.hset(mapKey, key, gson.toJson(value));
        expireTime = (expireTime == -1) ? RedisEnum.NORMAL_EXPIRE : expireTime;
        expire(mapKey, expireTime);
    }

    /**
     * 若redis无值，返回null ，若有值，则尝试转换类型
     * @param mapKey mapKey，通常是token
     * @param key key
     * @param c Class
     * @return
     */
    public <T> T getParseObjectFromMap(String mapKey, String key, Class<T> c) {
        String obj = jedisUtil.hget(mapKey, key);
        return StringUtils.isEmpty(obj) ? null : parseGson.fromJson(obj, c);

    }

    public String getStringFromMap(String mapKey, String key) {
        return jedisUtil.hget(mapKey, key);
    }

    /**
     * 设置过期时间
     */
    public void expire(String key, int expireTime) {
        jedisUtil.expire(key, expireTime);
    }

    /**
     * 刷新过期时间
     */
    public void refreshExpireTime(String key) {
        expire(key, RedisEnum.NORMAL_EXPIRE);
    }

    public Long delete(String key) {
        return jedisUtil.del(key);
    }

    public Long hDelete(String mapKey, String key) {
        return jedisUtil.hdel(mapKey, key);
    }
}
